"""rename_acp_spec_to_acs_and_acp_spec_hash_to_acs_hash

Revision ID: 841699557267
Revises: c34ab1646dce
Create Date: 2025-08-19 18:00:43.229889

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy import String, Text


# revision identifiers, used by Alembic.
revision: str = "841699557267"
down_revision: Union[str, None] = "c34ab1646dce"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###

    # 添加新的列
    op.add_column("agent", sa.Column("acs", sa.Text(), nullable=True))
    op.add_column("agent", sa.Column("acs_hash", sa.String(length=256), nullable=True))

    # 迁移数据：将 acp_spec 的数据复制到 acs，将 acp_spec_hash 的数据复制到 acs_hash
    op.execute("UPDATE agent SET acs = acp_spec WHERE acp_spec IS NOT NULL")
    op.execute(
        "UPDATE agent SET acs_hash = acp_spec_hash WHERE acp_spec_hash IS NOT NULL"
    )

    # 删除旧的列
    op.drop_column("agent", "acp_spec_hash")
    op.drop_column("agent", "acp_spec")
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###

    # 重新添加旧的列
    op.add_column(
        "agent", sa.Column("acp_spec", sa.TEXT(), autoincrement=False, nullable=True)
    )
    op.add_column(
        "agent",
        sa.Column(
            "acp_spec_hash", sa.VARCHAR(length=256), autoincrement=False, nullable=True
        ),
    )

    # 迁移数据：将 acs 的数据复制回 acp_spec，将 acs_hash 的数据复制回 acp_spec_hash
    op.execute("UPDATE agent SET acp_spec = acs WHERE acs IS NOT NULL")
    op.execute("UPDATE agent SET acp_spec_hash = acs_hash WHERE acs_hash IS NOT NULL")

    # 删除新的列
    op.drop_column("agent", "acs_hash")
    op.drop_column("agent", "acs")
    # ### end Alembic commands ###
